<template>
	<nt-container v-if="!loading">

		<view v-if="loadingError" class="loading-error">
			<nt-button @tap="getMemberInfo" color="#2D7FFD" text="加载失败，重新加载"></nt-button>
		</view>
		<view class="nt-mine" v-else>
			<view class="nt-mine-header">
				<view class="nt-mine-header__top">
					<image class="nt-mine-header__top--icon" src="/static/image/mine/id-info.png"
						@tap="nav('/subpackage/member/pages/index')"></image>
					<image class="nt-mine-header__top--icon" src="/static/image/mine/ewm.png"
						@tap.stop="nav('/subpackage/member/pages/sharing-code/index')"></image>
					<image class="nt-mine-header__top--icon" src="/static/image/mine/setting.png"
						@tap.stop="nav(`/subpackage/member/pages/setting/index`)"></image>
				</view>
				<view class="nt-mine-header__user" @tap="nav('/subpackage/member/pages/index')">
					<image src="" class="nt-mine-header__user--image" :src="userInfo.avatar"></image>
					<view class="nt-mine-header__user--info">
						<view class="nt-mine-header__user--info-name">
							{{ userInfo.username }}
							<image class="nt-mine-header__user--info-image" src="/static/image/mine/shifu.png"></image>
						</view>
						<image class="nt-mine-header__user--info-position" src="/static/image/mine/dengji.png"></image>
					</view>
				</view>
				<view class="nt-mine-header__statistics">
					<view class="nt-mine-header__statistics-item" v-for="(it, index) in monthData[0]" :key="index"
						@tap="toWebView(it)">
						<view class="nt-mine-header__statistics-item__num">{{it.value}}
						</view>
						<view class="nt-mine-header__statistics-item__label">{{ it.name }}</view>
					</view>
				</view>
			</view>
			<u-notice-bar v-if="userInfo.monthData&&userInfo.monthData.tips" class="notice-bar"
				:text="userInfo.monthData.tips" icon="" mode=""></u-notice-bar>
			<view class="nt-mine-charts">
				<view class="nt-mine-charts__data">
					<view class="nt-mine-charts__data-top">
						<view class="nt-mine-charts__data-top__item" v-for="(it, index) in monthData[1]" :key="index"
							@tap="toPage(it.event,it)">
							<view class="nt-mine-charts__data-top__item-wrap">
								<view class="nt-mine-charts__data-top__item-title">{{ it.name }}</view>
								<view class="nt-mine-charts__data-top__item-price">{{ it.value }}</view>
							</view>
						</view>
					</view>
					<view class="nt-mine-charts__data-bottom">
						<view class="nt-mine-charts__data-bottom__item">
							当前单量预计提高 -％
						</view>
						<view class="nt-mine-charts__data-bottom__item line">
							您所在的小组排名 第 {{ userInfo.monthData.group_ranking }} 名
						</view>
					</view>
				</view>
			</view>
			<view class="nt-mine-body">
				<view class="nt-card" v-for="(item, index) in userMenu" :key="index" v-if="item.status === 1">
					<view class="nt-card-title">{{ item.group_name }}</view>
					<u-grid :border="false" :col="4">
						<u-grid-item v-for="(it,index2) in item.list" :key="index2" @click="toWebView(it)">
							<view class="grid-icon">
								<nt-cdn-image :src="it.ico"></nt-cdn-image>
							</view>
							<text class="grid-text">{{it.name}}</text>
							<view class="grid-tip" v-if="it.showTip">{{it.tip}}</view>
						</u-grid-item>
					</u-grid>
				</view>
			</view>

		</view>
	</nt-container>
</template>

<script>
	import {
		mapGetters
	} from "vuex"
	import {
		getUserMenuConfig,
		getUserInfo
	} from "@/api"
	import {
		userMenuMap
	} from "@/utils/staticMap"
	export default {
		data() {
			return {
				userMenu: [],
				userInfo: {},
				loading: true,
				loadingError: false
			}
		},
		computed: {
			monthData() {
				const {
					fix_number,
					sendrepair_number,
					profit_average,
					integral,
					ranking_all,
					ranking_area
				} = this.userInfo.monthData
				const menu = this.userMenu.find(item => item.status === 0)
				console.log(menu)
				if (menu) {
					const res = menu.list.map(item => {
						return item.map(it => {
							it.value = this.userInfo.monthData[it.event]
							return it
						})
					})
					console.log(res, '----')
					return res;
				}
				return []
			}
		},
		onLoad() {
			this.getMemberInfo()
		},
		// onPullDownRefresh(){
		// 	uni.startPullDownRefresh({})
		// 	this.refresh()
		// },
		methods: {
			getMemberInfo() {
				getUserInfo("userCenter").then(res => {
					console.log(res, '用户信息')
					this.loading = false
					if (res.code === 1) {
						this.userInfo = res.data
						this.userMenu = res.data.menuGroupList || []
						return
					}
					this.loadingError = true
				})
			},
			toWebView(data) {
				console.log(data)
				if (data.path) {
					this.nav(data.path)
				} else if (data.keyurl) {
					// this.nav('/subpackage/member/pages/teamPoints/index')
					this.nav('/pages/webview/index?src=' + data.keyurl)

				}
			},
			toPage(type, it) {
				let url = type === "profit" ? "/subpackage/member/pages/achievement/index" :
					"/subpackage/member/pages/commission/commission"
				this.nav(it.path)
				// this.nav()
			},
			async refresh() {
				await this.$store.dispatch("user/getUserInfo")
				setTimeout(() => uni.stopPullDownRefresh(), 500)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.nt-mine {
		&-header {
			// height: 520rpx;
			background: var(--theme-color);
			position: relative;
			padding: 0 30rpx;
			box-sizing: border-box;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			// margin-bottom: 30rpx;

			&__top {
				display: flex;
				align-items: center;
				justify-content: flex-end;
				padding: 30rpx 0;
				box-sizing: border-box;

				&--icon {
					margin-left: 40rpx;
					width: 50rpx;
					height: 50rpx;
				}
			}

			&__user {
				display: flex;
				align-items: center;

				&--image {
					width: 100rpx;
					height: 100rpx;
					border-radius: 50%;
					background: #fff;
					border: 2rpx solid #9F9F9F;
				}

				&--info {
					color: #fff;
					padding-left: 24rpx;

					&-name {
						font-size: 36rpx;
						font-weight: bold;
					}

					&-image {
						width: 71rpx;
						height: 37rpx;
						margin-left: 10rpx;
						vertical-align: middle;
					}

					&-position {
						width: 180rpx;
						height: 36rpx;
						margin-top: 10rpx
					}
				}
			}

			&__statistics {
				width: 100%;
				// height: 135rpx;
				background: #fff;
				border-top-left-radius: 20rpx;
				border-top-right-radius: 20rpx;
				display: flex;
				align-items: center;
				// justify-content: space-between;
				flex-wrap: wrap;
				padding: 20rpx 0;
				box-sizing: border-box;

				&-item {
					width: 33.333%;
					text-align: center;
					font-weight: bold;
					padding: 10rpx 0;

					&__num {
						font-size: 40rpx;
						color: #424242;
					}

					&__label {
						font-size: 24rpx;
						color: #999;
						margin: 10rpx 0;
					}
				}
			}
		}

		&-charts {
			padding: 0 30rpx;

			&__data {
				height: 265rpx;
				border-radius: 20rpx;
				margin-bottom: 30rpx;
				background: #408DFC;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				overflow: hidden;

				&-top {
					display: flex;
					align-items: center;
					justify-content: space-between;
					height: calc(100% - 80rpx);

					&__item {
						flex: 1;
						padding: 0 10rpx;

						&-wrap {
							height: 135rpx;
							width: 300rpx;
							background: linear-gradient(to right, #79AEFD, #408DFC);
							border-radius: 10rpx;
							display: flex;
							align-items: flex-start;
							justify-content: center;
							flex-direction: column;
							color: #fff;
							padding-left: 30rpx;
							box-sizing: border-box;
						}

						&-title {
							font-size: 20rpx;
							margin-bottom: 10rpx;
						}

						&-price {
							font-size: 48rpx;
							font-weight: bold;
						}
					}
				}

				&-bottom {
					height: 80rpx;
					background: #4690FC;
					font-size: 22rpx;
					color: #fff;
					display: flex;
					justify-content: space-between;
					align-items: center;

					&__item {
						flex: 1;
						text-align: center;
						position: relative;

						&.line {
							&::before {
								content: "";
								width: 2rpx;
								height: 30rpx;
								background: #fff;
								position: absolute;
								left: 0;
								top: 50%;
								transform: translateY(-50%);
							}
						}
					}
				}
			}
		}

		&-body {
			padding: 0 30rpx;
		}

	}

	.loading-error {
		height: 100vh;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
	}

	.nt-card {
		border-radius: 20rpx;
		background: #fff;
		padding: 30rpx;
		box-sizing: border-box;
		margin-bottom: 30rpx;

		&-title {
			font-size: 32rpx;
			color: #333;
			font-weight: bold;
			margin-bottom: 40rpx;
		}

		/deep/.u-grid-item {
			position: relative;
		}

		.grid {
			&-icon {
				width: 50rpx;
				height: 50rpx;
			}

			&-text {
				font-size: 24rpx;
				color: #333;
				margin: 20rpx 0 50rpx;
				font-weight: bold;
			}

			&-tip {
				width: 60rpx;
				height: 36rpx;
				line-height: 36rpx;
				border-radius: 50rpx;
				border-bottom-left-radius: 0;
				background: #FF0500;
				color: #fff;
				font-size: 20rpx;
				text-align: center;
				position: absolute;
				right: 10rpx;
				top: -20rpx;
			}

		}
	}

	.notice-bar {
		margin: 10rpx 0;
	}
</style>